#include "SentimentBar.h"
#include "Renderer.h"
#include <sstream>
#include <iostream>

using namespace irr; 
using namespace core;
using namespace gui; 
using namespace video; 
using namespace scene; 
using namespace std;

SentimentBar::SentimentBar(IGUIStaticText* posText, IGUIStaticText* negText) : _posTextDesc(posText), _negTextDesc(negText)
{
	CRenderer* pRender = CRenderer::Instance();
	ISceneNode* pSceneNode;

	ISceneNode* textDesc = pRender->AddText("Sentiment Ratio", vector3df(0, 0, 0), SColor(255,255,255,255));
	
	textDesc->setName("SentimentBarLabel");
	textDesc->setVisible(false);
	
	_posBox = pRender->AddBox(vector3df(1.0f, 1.0f, 0.0f), 
									 vector3df(1.0f, 1.0f, 1.0f), SColor(255, 0, 0, 150));
	_posBox->setName("SentimentPos");
	_posBox->setVisible(false);

	_negBox = pRender->AddBox(vector3df(1.0f, 1.0f, 1.0f), 
									vector3df(1.0f, 1.0f, 1.0f), SColor(255, 200, 0, 0));
	_negBox->setName("SentimentNeg");
	_negBox->setVisible(false);

	_divider = pRender->AddBox(vector3df(-1.0f, 1.0f, 1.0f), 
									vector3df(1.0f, 1.0f, 1.0f), SColor(255, 100, 100, 100));
	_divider->setVisible(false);
}

void SentimentBar::SetSentiment(float pos, float neg)
{
	positivity = pos;
	negativity = -1*neg;
}

void SentimentBar::Update()
{
	CRenderer*		pRender		= CRenderer::Instance();
	ISceneManager*	pSceneMgr	= pRender->SceneManager();
	ISceneNode*		pSceneNode;

	std::wstringstream wss;

	wss << "Positivity: " << (int)(100*positivity);
	const wchar_t* posStr = wss.str().c_str();
	_posTextDesc->setText(posStr);	
	_posTextDesc->setVisible(true);
	wss.clear();
	wss.str(L"");

	std::wstringstream negSS;
	negSS << "Negativity: " << (int)(100*negativity);
	const wchar_t* negStr = negSS.str().c_str();
	_negTextDesc->setText(negStr);
	_negTextDesc->setVisible(true);
	negSS.clear();
	negSS.str(L"");

	int startX = -170;
	int startY = 35;

	int totalRectWidth = 11;
	int totalRectHeight = 40;

	float sentTotal = positivity+negativity;

	int posHeight = (int)((positivity/sentTotal)*totalRectHeight);
	int negHeight = totalRectHeight - posHeight;

	float midY1 = startY + posHeight/2;
	float midY2 = startY - negHeight/2;

	_divider->setPosition(vector3df(startX-25, startY, 0));
	_divider->setScale(vector3df(50,1,1));

	_posBox->setPosition(vector3df(startX, midY1, 1));
	_posBox->setScale(vector3df(totalRectWidth, posHeight, 1.0f));
	_negBox->setPosition(vector3df(startX, midY2, 1));
	_negBox->setScale(vector3df(totalRectWidth, negHeight, 1.0f));

	this->SetVisible(true);
}

void SentimentBar::SetVisible(bool visibility) {
	_posBox->setVisible(visibility);
	_negBox->setVisible(visibility);
	_posTextDesc->setVisible(visibility);
	_negTextDesc->setVisible(visibility);
	_divider->setVisible(visibility);
}

SentimentBar::~SentimentBar()
{
}


